const fetch = require('../../utils/fetch')
const WxParse = require('../../lib/wxParse/wxParse.js');
var app = getApp();
import Toast from '@vant/weapp/toast/toast';
import Cache from "../../utils/cache";
import Tool from "../../utils/tool";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isshowInput:false,//是否显示激活卡密dialog
    isruleOk:false,//是否正常加载规则明细
    isLoadOk:false,//判断onload是否执行完成，只有执行完成了onshow才可以再次网络请求，防止重复请求
    userInfo: {
      isLogin: false,
      username: "还未登录，请先登录！",
      usertype: undefined,
      starttime: undefined,
      endtime: undefined,
      wxid: undefined,
      headpic: undefined,
      days: undefined
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this; 
    fetch('/carrules')
      .then(res => {
        if (res.data.code === 200) {   
          that.setData({isruleOk:true}) 
           WxParse.wxParse('goodsDetail', 'html', res.data.data.detail, that);
        }else{ 
          that.setData({isruleOk:false}) 
        }
        that.setData({isLoadOk:true}) 
      }).catch(res => {
        that.setData({isLoadOk:true}) 
      });
    this.initLoginState();

  },
  onShow() { 
    this.updateDataInfo();
    //查看是否加载成功，失败则重新加载
    if(!this.data.isruleOk&&this.data.isLoadOk){ 
      let that = this;
      fetch('/carrules')
        .then(res => {
          if (res.data.code === 200) {  
            that.setData({isruleOk:true}) 
            WxParse.wxParse('goodsDetail', 'html', res.data.data.detail, that);
          }else{ 
            that.setData({isruleOk:false}) 
          } 
        });
    } 
   
  },
  inputCarid: function () { //执行卡密校验
    let that = this;
    if (!app.userInfo.isLogin) { //如果没登陆 则跳转至登录界面
      wx.navigateTo({
        url: '/pages/login/login'
      })
    } else {
      //展示激活对话框
      this.setData({isshowInput:true}) 
   
    }
  },
  // 登录操作
  getLoginUser: function () {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  updateDays: function () {
    if (this.data.userInfo.endtime && !Tool.isEmpty(this.data.userInfo.endtime)) {
      var dateStart =  new Date();
      var dateEnd = new Date(this.data.userInfo.endtime);
      var difValue = (dateEnd - dateStart) / (1000 * 60 * 60 * 24);
      this.setData({
        'userInfo.days': parseInt(difValue) 
      });
    }
  },
  updateDataInfo: function () {
    this.setData({
      'userInfo.isLogin': app.userInfo.isLogin,
      'userInfo.username': app.userInfo.username,
      'userInfo.usertype': app.userInfo.usertype,
      'userInfo.starttime': app.userInfo.starttime,
      'userInfo.endtime': app.userInfo.endtime,
      'userInfo.headpic': app.userInfo.headpic,
      'userInfo.wxid': app.userInfo.wxid
     
    });
    this.updateDays();
  }, // 验证登录状态
  initLoginState: async function () {
    if (app.userInfo.isLogin) { //已经登录

    } else { //去缓存中获取 登录信息 并赋值给全局变量
      const WXID = Cache.getCache(app.globalData.WXID);
      const USERTYPE = Cache.getCache(app.globalData.USERTYPE);
      const STARTTIME = Cache.getCache(app.globalData.STARTTIME);
      const ENDTIME = Cache.getCache(app.globalData.ENDTIME);
      const USERNAME = Cache.getCache(app.globalData.USERNAME);
      const HEADPIC = Cache.getCache(app.globalData.HEADPIC);
      if (!Tool.isEmpty(WXID)) {
        //登录状态 则进行赋值 
        app.userInfo.isLogin = true;
        app.userInfo.wxid = WXID;
        app.userInfo.headpic = HEADPIC; 
        app.userInfo.usertype = USERTYPE;
        app.userInfo.starttime = STARTTIME;
        app.userInfo.endtime = ENDTIME;
        app.userInfo.username = USERNAME;
      }
    }
  },
  //确认看点击的是确认还是取消按钮 
  iputComplete: function (params) {
    //隐藏激活卡界面 
    this.setData({isshowInput:false})
    if(params.detail.confirm){
     //点击了确认按钮
     //获取输入内容 
    //点击确认按钮--调用接口激活  
    var data = {
      wxid: app.userInfo.wxid,
      cardno: params.detail.data.trim()
    };
    fetch(`/updateCardInfo`, data, "post")
      .then(response => {
        if (response.data.code === 200) {
          Toast.success('激活成功!');
          Cache.setCache(app.globalData.USERTYPE, response.data.data.usertype);
          Cache.setCache(app.globalData.STARTTIME, response.data.data.starttime);
          Cache.setCache(app.globalData.ENDTIME, response.data.data.endtime);
          Cache.setCache(app.globalData.USERNAME, response.data.data.username);
          app.userInfo.usertype = response.data.data.usertype;
          app.userInfo.starttime = response.data.data.starttime;
          app.userInfo.endtime = response.data.data.endtime;
          app.userInfo.username = response.data.data.username;
          that.updateDataInfo();
        } else {
          Toast.fail(response.data.msg);
        }
      })
    }
  }
})